package com.scap.authen.service;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.dao.MenuMappingDao;
import com.scap.authen.entity.MenuMapping;
import com.scap.authen.entity.RoleApp;
import com.scap.authen.util.JDate;

@Service("voMenuMappingService")
@Transactional
public class MenuMappingServiceImpl implements MenuMappingService{
	@Autowired
	ApplicationContext voContext;
	
	@Autowired
	MenuMappingDao voMenuMappingDao;

	@Override
	public String saveMenuMapping(String vaSiteId, String vaRoleId, String vaApplicationId, String voMenuId, HttpServletRequest request) {
		String vaResult = "";
		Locale voLocale = RequestContextUtils.getLocale(request);
		String[] voMenuMappingArray =  voMenuId.split("/");
		for ( int i = 0; i < voMenuMappingArray.length; i++){				
			MenuMapping voMenuMapping = new MenuMapping();
			voMenuMapping.setVaApplicationId(vaApplicationId);
			voMenuMapping.setVaRoleId(vaRoleId);
			voMenuMapping.setVaSiteId(vaSiteId);
			voMenuMapping.setVaInsertDate(JDate.getDate());
			voMenuMapping.setVaInsertTime(JDate.getTime());
			voMenuMapping.setVnInsertUserId(1);
			voMenuMapping.setVaCreateOperation("N");
			voMenuMapping.setVaReadOperation("N");
			voMenuMapping.setVaUpdateDate("N");
			voMenuMapping.setVaDeleteOperation("N"); 
			
			String vaTempMenuId = voMenuMappingArray[i].substring(0,2);
			voMenuMapping.setVaMenuId(vaTempMenuId);
			String vaTempCRUD = "";
			try {
				vaTempCRUD = voMenuMappingArray[i].substring(2);
				for ( int n = 0; n < vaTempCRUD.length(); n++){
					String vaCharAt = vaTempCRUD.charAt(n)+"";
					if ( vaCharAt.equals("C")){
						voMenuMapping.setVaCreateOperation("Y");
					} else if ( vaCharAt.equals("R")){
						voMenuMapping.setVaReadOperation("Y");
					} else if ( vaCharAt.equals("U")){
						voMenuMapping.setVaUpdateOperation("Y");
					} else if ( vaCharAt.equals("D")){
						voMenuMapping.setVaDeleteOperation("Y");
					}
				}					
			} catch (Exception e) {
				e.printStackTrace();
			}
			voMenuMappingDao.insertMenuMapping(voMenuMapping);
		}
		return vaResult;
	}

	@Override
	public boolean deleteMenuMapping(String vaSiteId, String vaRoleId,String vaApplicationId) {
		return voMenuMappingDao.deleteMenuMapping(vaSiteId ,vaRoleId ,vaApplicationId);
	}

	@Override
	public String findMenuChecked(String vaAppId, String roleId, String siteId) {
		String voMenuChecked = "";
		List<MenuMapping> voMenuCheckedtList = voMenuMappingDao.findMenuChecked(vaAppId, roleId, siteId);
		if (voMenuCheckedtList.size() != 1) {
			for(MenuMapping voTempAppChecked: voMenuCheckedtList){
				String vaOneRoeData = "";
				vaOneRoeData = voTempAppChecked.getVaMenuId()+","+voTempAppChecked.getVaCreateOperation()+","
				+voTempAppChecked.getVaReadOperation()+","+voTempAppChecked.getVaUpdateOperation()+" "+voTempAppChecked.getVaDeleteOperation();
				voMenuChecked +=vaOneRoeData+"/";
			}
		} else {
			voMenuChecked = voMenuCheckedtList.get(0).getVaMenuId()+","+voMenuCheckedtList.get(0).getVaCreateOperation()+","
							+voMenuCheckedtList.get(0).getVaReadOperation()+","+voMenuCheckedtList.get(0).getVaUpdateOperation()+","
							+voMenuCheckedtList.get(0).getVaDeleteOperation();
		}			
		return voMenuChecked;
	}
}
